package com.wjb.xietong.server.crm;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wjb.xietong.server.Msg;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class SalesInfo extends RequestBase{

	private LinkedList<SalesData> listSales = null;;
	private long sellerID = 0;
	private String date = null;
	
	
	public SalesInfo(){
		msgType = Msg.TYPE_CRM_SALES;
	}
	
	
	public Msg requestInfo(LinkedList<SalesData> list, long slID, String dt){
		listSales = list;
		sellerID = slID;
		date = dt;
		
		requsetInfo(IDs.CRM_URL);
		
		return getMsg();
	}

	
	@Override
	public int jsonGetResp(JSONObject resp){
		if (!checkRespCode(resp)) {
			// TODO
			return -1;
		}
		try {
			if (resp.has(IDs.CRM_SALE_MAP_LIST)) {
				JSONArray array = resp.getJSONArray(IDs.CRM_SALE_MAP_LIST);
				if (null == array) {
					LogD.output("Warning: No Data!");
					return -3;
				}

				for (int i = 0; i < array.length(); i++) {
					JSONObject jsonObj = array.getJSONObject(i);
					if (null == jsonObj) {
						continue;
					}

					SalesData data = new SalesData();
					if(jsonObj.has(IDs.CRM_SALE_ITEM_ID)){
						data.itemID = jsonObj.getLong(IDs.CRM_SALE_ITEM_ID);
					}
					if(jsonObj.has(IDs.CRM_SALE_ITEM_NAME)){
						data.itemName = new String(jsonObj.getString(IDs.CRM_SALE_ITEM_NAME));
					}
					if(jsonObj.has(IDs.CRM_SALE_TRADE_NUM)){
						data.tradeNum = jsonObj.getLong(IDs.CRM_SALE_TRADE_NUM);
					}
					if(jsonObj.has(IDs.CRM_SALE_PRICE)){
						data.price = jsonObj.getDouble(IDs.CRM_SALE_PRICE);
					}
					if(jsonObj.has(IDs.CRM_SALE_TRADE_PAYMENT)){
						data.tradePayment = jsonObj.getDouble(IDs.CRM_SALE_TRADE_PAYMENT);
					}
					if(jsonObj.has(IDs.CRM_SALE_INDEX)){
						data.index = jsonObj.getLong(IDs.CRM_SALE_INDEX);
					}
					if(jsonObj.has(IDs.CRM_SALE_IMAGE)){
						data.imageURL = new String(jsonObj.getString(IDs.CRM_SALE_IMAGE));
					}
					listSales.add(data);
				}
			}
		} catch (JSONException e) {
			LogD.output("Exception:" + e.getMessage());
			return -2;
		}
		return 0;
	}
	
	@Override
	public Map<String, String> getRqstMap(){
		Map<String, String> map = new HashMap<String, String>();
		map.put(IDs.M, IDs.NOTIFY_CRM_TOPSALE);
		map.put(IDs.CRM_SELLER_ID, String.format("%d", sellerID));
		map.put(IDs.CRM_SELLER_DATE, date);
		return map;
	}

	
	public class SalesData{
		public long 	itemID;
		public String 	itemName;
		public long 	tradeNum;
		public double   price;
		public double   tradePayment;
		public long 	index;
		public String   imageURL;
		
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean parseJsonObj(JSONObject response) {
		// TODO Auto-generated method stub
		return false;
	}
	
}
